﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>OnExit - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The OnExit function specifies a callback function or subroutine to run automatically when the script exits." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>OnExit</h1>

<p>Specifies a <a href="../Functions.htm">function</a> to run automatically when the script exits.</p>

<pre class="Syntax"><span class="func">OnExit</span> Func <span class="optional">, AddRemove</span></pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Func</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>或<a href="../Concepts.htm#objects">对象</a></p>
    <p>A function name or <a href="../objects/Functor.htm">function object</a> to call when the script is exiting. The function can optionally define parameters as shown below. If an OnExit function returns a non-zero integer, the script does not exit. Otherwise, the script exits after all registered functions are called.</p>
    <pre class="Syntax"><i>MyFunction</i>(<a href="#ExitReason">ExitReason</a>, <a href="ExitApp.htm">ExitCode</a>)</pre>
  </dd>
  
  <dt>AddRemove</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>下列值之一:<br>
  <strong>1</strong> (the default): Call the function after any previously registered functions.<br>
  <strong>-1</strong>: Call the function before any previously registered functions.<br>
  <strong>0</strong>: Do not call the function.</p>
  </dd>

</dl>

<h2 id="Remarks">备注</h2>
<p>Any number of OnExit functions can be registered. An OnExit function usually should not call ExitApp; if it does, the script terminates immediately.</p>
<p>OnExit functions are called when the script exits by any means (except when it is killed by something like &quot;End Task&quot;). It is also called whenever <a href="_SingleInstance.htm">#SingleInstance</a> and <a href="Reload.htm">Reload</a> ask a previous instance to terminate.</p>
<p>A script can detect and optionally abort a system shutdown or logoff via <code>OnMessage(0x11, &quot;WM_QUERYENDSESSION&quot;)</code>(see <a href="OnMessage.htm#ExShutdown">OnMessage example #2</a> for a working script).</p>
<p>The OnExit <a href="../misc/Threads.htm">thread</a> does not obey <a href="_MaxThreads.htm">#MaxThreads</a> (it will always launch when needed). In addition, while it is running, it cannot be interrupted by any <a href="../misc/Threads.htm">thread</a>, including <a href="../Hotkeys.htm">hotkeys</a>, <a href="../objects/Menu.htm">custom menu items</a>, and <a href="SetTimer.htm">timed subroutines</a>. However, it will be interrupted (and the script will terminate) if the user chooses Exit from the tray menu or main menu, or the script is asked to terminate as a result of <a href="Reload.htm">Reload</a> or <a href="_SingleInstance.htm">#SingleInstance</a>. Because of this, an OnExit function should be designed to finish quickly unless the user is aware of what it is doing.</p>
<p>If the OnExit <a href="../misc/Threads.htm">thread</a> encounters a failure condition such as a runtime error, the script will terminate.</p>
<p>If the OnExit <a href="../misc/Threads.htm">thread</a> was launched due to an <a href="Exit.htm">Exit</a> or <a href="ExitApp.htm">ExitApp</a> function that specified an exit code, that exit code is used unless an OnExit function returns true (preventing exit) or calls ExitApp.</p>
<p>Whenever an exit attempt is made, each OnExit function starts off fresh with the default values for settings such as <a href="SendMode.htm">SendMode</a>. These defaults can be changed in the <a href="../Scripts.htm#auto">auto-execute section</a>.</p>
<p>If an OnExit function declares parameters, its first parameter is one of the following words:</p>
<table class="info" id="ExitReason">
  <tr id="logoff">
    <td>Logoff</td>
    <td>The user is logging off.</td>
  </tr>
  <tr>
    <td>Shutdown</td>
    <td>The system is being shut down or restarted, such as by the <a href="Shutdown.htm">Shutdown</a> function.</td>
  </tr>
  <tr>
    <td>Close</td>
    <td>The script was sent a WM_CLOSE or WM_QUIT message, had a critical error, or is being closed in some other way. Although all of these are unusual, WM_CLOSE might be caused by <a href="WinClose.htm">WinClose</a> having been used on the script's main window. To close (hide) the window without terminating the script, use <a href="WinHide.htm">WinHide</a>.</td>
  </tr>
  <tr>
    <td>Error</td>
    <td>A runtime error occurred in a script that is not <a href="_Persistent.htm">persistent</a>. An example of a runtime error is <a href="Run.htm">Run/RunWait</a> being unable to launch the specified program or document.</td>
  </tr>
  <tr>
    <td>Menu</td>
    <td>The user selected Exit from the main window's menu or from the standard tray menu.</td>
  </tr>
  <tr>
    <td>Exit</td>
    <td>The <a href="Exit.htm">Exit</a> or <a href="ExitApp.htm">ExitApp</a> function was used (includes <a href="../objects/Menu.htm">custom menu items</a>).</td>
  </tr>
  <tr>
    <td>Reload</td>
    <td>The script is being reloaded via the <a href="Reload.htm">Reload</a> function or menu item.</td>
  </tr>
  <tr>
    <td>Single</td>
    <td>The script is being replaced by a new instance of itself as a result of <a href="_SingleInstance.htm">#SingleInstance</a>.</td>
  </tr>
</table>

<h2 id="Related">相关</h2>
<p><a href="OnError.htm">OnError</a>, <a href="OnMessage.htm">OnMessage</a>, <a href="CallbackCreate.htm">CallbackCreate</a>, <a href="OnClipboardChange.htm">OnClipboardChange</a>, <a href="ExitApp.htm">ExitApp</a>, <a href="Shutdown.htm">Shutdown</a>, <a href="_Persistent.htm">#Persistent</a>, <a href="../misc/Threads.htm">Threads</a>, <a href="Return.htm">Return</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExAdvanced">
<p><a href="#ExAdvanced">#1</a>: The following example uses <a href="_Persistent.htm">#Persistent</a> to prevent the script from exiting automatically. After running the script, right click the tray icon and click <em>Exit</em> to test the OnExit function. Then click "Yes" to terminate the script or "No" to keep it running.</p>
<pre>#Persistent

<em>; Register a function to be called on exit:</em>
OnExit("ExitFunc")

<em>; Register an object to be called on exit:</em>
OnExit(<a href="ObjBindMethod.htm">ObjBindMethod</a>(MyObject, "Exiting"))

ExitFunc(ExitReason, ExitCode)
{
    if ExitReason != "Logoff" and ExitReason != "Shutdown"
    {
        Result := MsgBox("Are you sure you want to exit?",, 4)
        if Result = "No"
            return 1  <em>; OnExit functions must return non-zero to prevent exit.</em>
    }
    <em>; Do not call ExitApp -- that would prevent other OnExit functions from being called.</em>
}

class MyObject
{
    Exiting()
    {
        MsgBox "MyObject is cleaning up prior to exiting..."
        <em>/*
        this.SayGoodbye()
        this.CloseNetworkConnections()
        */</em>
    }
}</pre>
</div>

</body>
</html>